package com.yuntsg.nnsfcp.service.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuntsg.nnsfcp.entity.ParseEntity;
import com.yuntsg.nnsfcp.front.entity.ParsingTxt;
import com.yuntsg.nnsfcp.front.entity.Searchfile;
import com.yuntsg.nnsfcp.front.entity.Searchinfo;
import com.yuntsg.nnsfcp.front.entity.Wosdetail;
import com.yuntsg.nnsfcp.front.mapper.*;
import com.yuntsg.nnsfcp.service.front.impl.SearchFileServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 根据数据库类型 <计算被引>
 *
 * @author ：ljx
 * @date ：Created in 2019/7/4
 */
@Slf4j
public abstract class AbstractCalculateCite implements Serializable {

    @Resource
    protected OrderService orderService;
    @Resource
    protected OrderBaseInfoMapper orderBaseInfoMapper;
    @Resource
    protected WosdetailMapper wosdetailMapper;
    @Resource
    protected SearchfileMapper searchfileMapper;
    @Resource
    protected SearchinfoMapper searchinfoMapper;
    @Resource
    protected ParsingTxtMapper parsingTxtMapper;
    @Resource
    protected UserInfoMapper userInfoMapper;
    @Resource
    protected SearchFileServiceImpl searchFileServiceImpl;

    /**
     * 自引次数
     */
    protected int selfCite = 0;
    /**
     * 自引列表集合
     */
    protected List<ParseEntity> selfCiteList = null;
    /**
     * 排除当前文章中的 所有作者
     */
    protected List<ParseEntity> eliminateAllList = null;
    /**
     * 作者本人外的他引列表集合
     */
    protected List<ParseEntity> citedList = null;
    /**
     * 总引用列表
     */
    protected List<ParseEntity> totleList = null;
    /**
     * 团队合作者外的他引列表集合
     */
    protected List<ParseEntity> collaboratorsList = null;
    /**
     * wos总被引列表
     */
    protected List<ParseEntity> wosQuoteList = null;


    /**
     * 封装数据
     *
     * @return
     */
    protected ParseEntity addParse(ParsingTxt parsingTxt) {
        //封装列表
        ParseEntity parseEntity = new ParseEntity();
        parseEntity.setTitle(parsingTxt.getTi());
        parseEntity.setSource(parsingTxt.getSo());
        parseEntity.setAuthor(parsingTxt.getAu());
        parseEntity.setYear(String.valueOf(parsingTxt.getPy()));
        parseEntity.setVolume(parsingTxt.getVl());
        parseEntity.setIssue(parsingTxt.getIss());
        if (parsingTxt.getBp() != null && parsingTxt.getEp() != null) {
            parseEntity.setPage(parsingTxt.getBp() + "-" + parsingTxt.getEp());
        }
        parseEntity.setDoi(parsingTxt.getDi());
        return parseEntity;
    }

    /**
     * 出现错误的处理方法
     *
     * @param wosDetail 本地wos实体
     */
    protected void myHandlerException(Wosdetail wosDetail) {
        Searchinfo searchinfo
                = searchinfoMapper.selectOne(new QueryWrapper<Searchinfo>()
                .eq("wos_id", wosDetail.getId()));
        if (searchinfo != null) {
            if (!searchinfo.getStatus().equals(1)) {
                log.info("info的状态 != 1, 未能成功获取被引次数");
                wosDetail.setCiteStatus(String.valueOf(searchinfo.getStatus()));
                wosdetailMapper.updateById(wosDetail);
            } else {
                Searchfile searchfile
                        = searchfileMapper.selectOne(new QueryWrapper<Searchfile>()
                        .eq("sid", searchinfo.getSid()));
                if (searchfile == null) {
                    //log.info("貌似没有抓到被引, 尝试重新获取");
                    /*searchinfo.setStatus(0);
                    searchinfoMapper.updateById(searchinfo);*/
                    log.info("状态-99__获取失败.. ");
                    wosDetail.setCiteStatus("-99");
                    wosdetailMapper.updateById(wosDetail);
                }
                searchfile = null;
            }
        }
    }

    /**
     * 删除所有 计算过的数据
     * 避免 同一订单下. 重新计算,导致数据重复计算
     *
     * @param txtList 被引数据
     */
    protected void deleteCitedTxt(List<ParsingTxt> txtList) {
        if (txtList != null && txtList.size() > 0) {
            log.info("====删除计算依赖数据=====");
            for (ParsingTxt parsingTxt : txtList) {
                Integer txtId = parsingTxt.getPid();

                List<Searchfile> fileList
                        = searchfileMapper.selectList(new QueryWrapper<Searchfile>()
                        .eq("fid", txtId));

                if (fileList.size() > 0) {
                    for (Searchfile searchfile : fileList) {
                        Integer fid = searchfile.getFid();
                        Integer sid = searchfile.getSid();

                        List<Searchinfo> infoList
                                = searchinfoMapper.selectList(new QueryWrapper<Searchinfo>()
                                .eq("sid", sid));

                        if (infoList.size() > 0) {
                            for (Searchinfo searchinfo : infoList) {
                                Integer sid1 = searchinfo.getSid();
                                searchinfoMapper.deleteById(sid1);
                            }
                        }
                        searchfileMapper.deleteById(fid);
                        infoList = null;
                    }
                }
                parsingTxtMapper.deleteById(txtId);
                fileList = null;
            }
        }
        txtList = null;
    }

    /**
     * 计算被引(抽象方法)
     *
     * @param wosdetail 本地wos实体
     * @param boo       true 为wos的总被引, false为用户选择的
     */
    public abstract void runCalculateCite(Wosdetail wosdetail, boolean boo);

}
